package com.dsteiner.xelavos.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.Widgets.IHTML;
import com.dsteiner.widgets.client.Widgets.IImage;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.controller.PopupOverlayController;
import com.dsteiner.widgets.client.model.PopupOverlayModel;
import com.dsteiner.xelavos.client.constants.Constants;
import com.dsteiner.xelavos.client.domain.User;
import com.dsteiner.xelavos.client.model.DisplayModel;
import com.dsteiner.xelavos.client.model.UserStatsModel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

public class UserStatsController {

	private UserStatsModel userStatsModel;
	private IHTML infoHtml;
	private IImage lastLoggedInUserImage;
	private IHTML lastLoggedInUserHtml;
	private DisplayModel userDisplayModel;
	private PopupOverlayModel userPopupOverlayModel;
	private IWidget userPanel;

	public void registerInfoHtml(IHTML infoHtml) {
		this.infoHtml = infoHtml;
	}

	public void registerLastLoggedInUserHtml(IHTML lastLoggedInUserHtml) {
		this.lastLoggedInUserHtml = lastLoggedInUserHtml;
		lastLoggedInUserHtml.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				PopupOverlayController.showAtCenter(userPopupOverlayModel);
			}
		});
	}

	public void registerLastLoggedInUserImage(IImage lastLoggedInUserImage) {
		this.lastLoggedInUserImage = lastLoggedInUserImage;
	}

	public void registerUserStatsModel(UserStatsModel userStatsModel) {
		this.userStatsModel = userStatsModel;
		userStatsModel.addPropertyChangeListener("countLoggedIn", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateInfoHtml();
			}
		});
		userStatsModel.addPropertyChangeListener("countOnline", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateInfoHtml();
			}
		});
		userStatsModel.addPropertyChangeListener("lastLoggedIn", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateLastLoggedInUser();
			}
		});
	}

	public void registerUserStatsWidget(ILazyWidget userStatsWidget) {
		userStatsWidget.addWidgetListener(new WidgetListenerAdapter() {
			@Override
			public void onAttach(IWidget widget) {
				updateInfoHtml();
				updateLastLoggedInUser();
			}
		});
	}

	private void updateInfoHtml() {
		int countLoggedIn = userStatsModel.getCountLoggedIn();
		int countOnline = userStatsModel.getCountOnline();
		int countGuest = countOnline - countLoggedIn;
		
		infoHtml.setHTML("<strong>"+countGuest+"</strong> "+(countGuest == 1 ? "usuario" : "usuarios")
				+" online<strong>   "
				+countLoggedIn+"</strong> "+(countLoggedIn == 1 ? "miembro" : "miembros")+" de Xelavos online");
	}

	private void updateLastLoggedInUser() {
		User lastLoggedIn = userStatsModel.getLastLoggedIn();
		userPanel.setVisible(lastLoggedIn != null);
		if(lastLoggedIn != null){
			lastLoggedInUserImage.setUrl(Constants.Util.getImageUrl(lastLoggedIn.getImageId()));
			lastLoggedInUserHtml.setHTML("<a href=\"javascript:void(0);\">"+lastLoggedIn.getFirstName()+"</a>");
			userDisplayModel.setModelObject(lastLoggedIn);
		}
	}

	public void registerUserDisplayModel(DisplayModel userDisplayModel) {
		this.userDisplayModel = userDisplayModel;
	}

	public void registerUserPopupOverlayModel(PopupOverlayModel userPopupOverlayModel) {
		this.userPopupOverlayModel = userPopupOverlayModel;
	}

	public void registerUserPanel(IWidget userPanel) {
		this.userPanel = userPanel;
	}
}